package com.testdocs.client.project.dashboard;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.testdocs.client.ClientFactory;
import com.testdocs.client.project.ProjectInfo;
import com.testdocs.client.project.ProjectService;
import com.testdocs.client.project.ProjectServiceAsync;
import com.testdocs.client.project.event.ProjectRemovedEvent;
import com.testdocs.client.project.event.ProjectSelectedEvent;
import com.testdocs.client.project.i18n.ProjectConstants;
import com.testdocs.client.project.initialselect.SelectProjectPlace;

public class DashboardActivity extends AbstractActivity implements DashboardView.Presenter {
	String projectId;
	ProjectInfo project;
	ClientFactory clientFactory;

	public DashboardActivity(DashboardPlace place, ClientFactory clientFactory) {
		this.projectId = place.getProjectId();
		this.clientFactory = clientFactory;
		clientFactory.getDashboardView().setPresenter(this);
	}
	
	@Override
	public void start(final AcceptsOneWidget panel, EventBus eventBus) {
		loadProjectInfo(panel);
	}
	
	private void loadProjectInfo(final AcceptsOneWidget panel) {
		if (clientFactory.getProjectContextHolder().getProject() == null) {
			ProjectConstants projectConstants = (ProjectConstants) GWT.create(ProjectConstants.class);
			
			clientFactory.getWaitingPopupPanel().show(projectConstants.loadingProjectInfoStatus());
			ProjectServiceAsync projectService = GWT.create(ProjectService.class);				
			projectService.getProjectInfo(projectId, new AsyncCallback<ProjectInfo>() {
				@Override
				public void onFailure(Throwable caught) {
					clientFactory.getRpcErrorHandler().handle(caught);
					clientFactory.getWaitingPopupPanel().hide();
				}
				@Override
				public void onSuccess(ProjectInfo result) {
					clientFactory.getEventBus().fireEvent(new ProjectSelectedEvent(result));
					clientFactory.getDashboardView().setProjectName(result.getName());
					panel.setWidget(clientFactory.getDashboardView());
					clientFactory.getWaitingPopupPanel().hide();
					project = result;
				}
			});
		} else {
			panel.setWidget(clientFactory.getDashboardView());
			project = clientFactory.getProjectContextHolder().getProject();		
			clientFactory.getDashboardView().setProjectName(project.getName());
		}
	}

	@Override
	public void onRemoveProject() {
		ProjectConstants projectConstants = (ProjectConstants) GWT.create(ProjectConstants.class);
		
		clientFactory.getWaitingPopupPanel().show(projectConstants.removingProjectStatus());
		ProjectServiceAsync projectService = GWT.create(ProjectService.class);
		projectService.removeProject(project, new AsyncCallback<Void>() {
			@Override
			public void onFailure(Throwable caught) {
				clientFactory.getRpcErrorHandler().handle(caught);
				clientFactory.getWaitingPopupPanel().hide();
			}

			@Override
			public void onSuccess(Void result) {
				clientFactory.getEventBus().fireEvent(new ProjectRemovedEvent(project));
				clientFactory.getWaitingPopupPanel().hide();
				clientFactory.getPlaceController().goTo(new SelectProjectPlace());
			}
		});
	}
}
